﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using WinSat;

namespace Smart
{
    public class GridView : System.Windows.Forms.DataGrid, ISupportInitialize
    {
        List<DataGridTextBoxColumn> list;
        private DataGridTableStyle gridStyle;

        public int RowCount
        {
            get
            {
                if (this.DataSource != null)
                {
                    if (this.DataSource is DataTable)
                        return ((DataTable)this.DataSource).Rows.Count;
                    else if (this.DataSource is DataView)
                        return ((DataView)this.DataSource).Table.Columns.Count;
                    else
                        return this.VisibleRowCount;
                }
                else
                {
                    return this.VisibleRowCount;
                }
            }
        }

        public GridView()
        {
            list = new List<DataGridTextBoxColumn>();
            gridStyle = new DataGridTableStyle();
        }

        public void AutoSizeCol(int col)
        {
            float width = 0;
            int numRows = this.RowCount;

            IntPtr hwnd = this.Handle;
            Graphics g = this.CreateGraphics();

            StringFormat sf = new StringFormat();
            SizeF size;

            for (int i = 0; i < numRows; ++i)
            {
                size = g.MeasureString(this[i, col].ToString(), this.Font);
                if (size.Width > width)
                    width = size.Width;
            }

            g.Dispose();

            this.TableStyles[0].GridColumnStyles[col].Width = (int)width; //(int)width + 8;
        }
        public void AutoSizeCol()
        {
            for (int i = 0; i < this.VisibleColumnCount; i++)
            {
                AutoSizeCol(i);
            }
        }

        public List<DataGridTextBoxColumn> Columns
        {
            get
            {
                return list;
            }
        }

        public string SelectedValue(int columnIndex)
        {
            if (this.DataSource != null)
                return this[this.CurrentRowIndex, columnIndex].ToString();
            else
                return string.Empty;
        }

        public object VeriKaynagi
        {
            get
            {
                return this.DataSource;
            }
            set
            {
                this.DataSource = value;
                DataSourceBind();
            }
        }

        void DataSourceBind()
        {
            this.TableStyles.Clear();
            this.gridStyle.GridColumnStyles.Clear();
            list.Clear();
            this.GridStyleColums.GridColumnStyles.Clear();
            if (this.DataSource is DataTable)
            {
                for (int i = 0; i < ((DataTable)this.DataSource).Columns.Count; i++)
                {
                    AddColumn(((DataTable)this.DataSource).Columns[i].Caption, ((DataTable)this.DataSource).Columns[i].ColumnName, ((DataTable)this.DataSource).Columns[i].Caption.Length * 10);
                }
            }
            else if (this.DataSource is DataView)
            {
                for (int a = 0; a < ((DataView)this.DataSource).Table.Columns.Count; a++)
                {
                    AddColumn(((DataView)this.DataSource).Table.Columns[a].Caption, ((DataView)this.DataSource).Table.Columns[a].ColumnName, ((DataView)this.DataSource).Table.Columns[a].Caption.Length * 10);
                }
            }
            this.TableStyles.Add(this.gridStyle);
        }

        public decimal GetRowToplam(int ColumnIndex)
        {
            decimal sum = 0;
            if (this.DataSource != null)
            {
                if (this.DataSource is DataTable)
                {
                    for (int i = 0; i < ((DataTable)this.DataSource).Rows.Count; i++)
                    {
                        sum = sum + Cevir.Parse<decimal>(((DataTable)this.DataSource).Rows[i][ColumnIndex], 0);
                    }
                }
                else if (this.DataSource is DataView)
                {
                    for (int a = 0; a < ((DataView)this.DataSource).Table.Columns.Count; a++)
                    {
                        sum = sum + Cevir.Parse<decimal>(((DataView)this.DataSource).Table.Rows[a][ColumnIndex], 0);
                    }
                }
            }
            return sum;
        }

        public DataGridTableStyle GridStyleColums
        {
            get { return gridStyle; }
            set { gridStyle = value; }
        }

        private void AddColumn(string headerText, string mappingName, int width)
        {
            DataGridTextBoxColumn Columns = new DataGridTextBoxColumn();
            Columns.HeaderText = headerText;
            Columns.MappingName = mappingName;
            Columns.Width = width;
            list.Add(Columns);
            gridStyle.GridColumnStyles.Add(Columns);
        }

        public void RemoveRow(int rowNum)
        {
            if (this.VeriKaynagi != null)
            {
                if (VeriKaynagi is DataTable)
                {
                    ((DataTable)VeriKaynagi).Rows.RemoveAt(rowNum);
                    this.Refresh();
                }
            }
        }

        #region ISupportInitialize Members

        public void BeginInit()
        {

        }

        public void EndInit()
        {

        }

        #endregion
    }

    public class ColumnStyle
    {
        public string Header { get; private set; }
        public string Column { get; private set; }
        public int Width { get; private set; }

        public ColumnStyle(string header, string column, int width)
        {
            Header = header;
            Column = column;
            Width = width;
        }

        public ColumnStyle(string column, int width)
        {
            Column = column;
            Header = column;
            Width = width;
        }
    }
}
